package com.jiang.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.jiang.demo.aspect.AuthResources;
import com.jiang.demo.common.MSG;
import com.jiang.demo.aspect.ApiIn;
import com.jiang.demo.entity.UserEntity;
import com.jiang.demo.common.enums.GenderEnum;
import com.jiang.demo.service.ApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "api接口测试")
@RestController
@RequestMapping("/api/demo")
public class ApiController {

    @Autowired
    private ApiService apiService;

    @AuthResources(value = {"xxx","aaa"})
    @ApiOperation(value = "查询所有用户")
    @GetMapping("/list")
    public MSG<List<UserEntity>> list() {
        List<UserEntity> list = new ArrayList<>();
        UserEntity jiang1 = UserEntity.builder().id(1L).name("jiang1").gender(GenderEnum.DEFAULT).build();
        jiang1.setCreateTime(LocalDateTime.now());
        jiang1.setBirth(LocalDate.now());
        list.add(jiang1);

        UserEntity jiang2 = UserEntity.builder().id(2L).name("jiang2").gender(GenderEnum.WOMAN).build();
        jiang2.setCreateTime(LocalDateTime.now());
        list.add(jiang2);

        UserEntity jiang3 = UserEntity.builder().id(3L).name("jiang3").gender(GenderEnum.MAN).build();
        jiang3.setCreateTime(LocalDateTime.now());
        list.add(jiang3);
        return MSG.SUCCESS(list);
    }

    @ApiOperation(value = "查询用户")
    @GetMapping("/{id}")
    public MSG<UserEntity> get(@PathVariable("id") Long id) {
        UserEntity user = apiService.getUser(id);
        return MSG.SUCCESS(user);
    }


    @ApiOperation(value = "添加用户")
    @PostMapping("/")
    public MSG<?> add(@ApiIn({"name", "age", "gender"})
                          @RequestBody UserEntity user) {
        log.trace("添加用户"+ JSONObject.toJSONString(user));
        log.debug("添加用户"+ JSONObject.toJSONString(user));
        log.info ("添加用户"+ JSONObject.toJSONString(user));
        log.warn ("添加用户"+ JSONObject.toJSONString(user));
        log.error("添加用户"+ JSONObject.toJSONString(user));

        apiService.exec();
        return MSG.SUCCESS();
    }

    @ApiOperation(value = "编辑用户")
    @PutMapping("/{id}")
    public MSG<?> update(@RequestBody @Valid UserEntity user) {
        log.info("编辑用户"+ JSONObject.toJSONString(user));
        return MSG.SUCCESS();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/{id}")
    public MSG<?> delete(@PathVariable("id") Long id) {
        return MSG.SUCCESS();
    }

}
